package ar.edu.itba.it.paw.web.user;

import java.awt.Panel;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import ar.edu.itba.it.paw.domain.EntityModel;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;

public class EditUserPage extends BasePage {


	private TextField<String> first_name_field;
	private TextField<String> last_name_field;
	private TextField<String> secret_question_field;
	private TextField<String> secret_answer_field;
	private PasswordTextField current_password_field;
	private PasswordTextField new_password_field;
	private PasswordTextField new_password_confirmation_field;
	
	private transient String current_password;
	private transient String new_password;
	private transient String new_password_confirmation;
	
	public EditUserPage(){
				
		User aux_user = MercadoLibreSession.get().getUser();
		
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<User> form = new Form<User>("editUserForm", new CompoundPropertyModel<User>(new EntityModel<User>(User.class,MercadoLibreSession.get().getUser()))) {
			@Override
			protected void onSubmit() {
				if(new_password != null){
					MercadoLibreSession.get().getUser().setPassword(new_password);
				}
				setResponsePage(UserMenuPage.class);
			}
		};
		
		first_name_field = new TextField<String>("first_name");
		first_name_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(first_name_field);
		last_name_field = new TextField<String>("last_name");
		last_name_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(last_name_field);
		secret_question_field = new TextField<String>("secret_question");
		secret_question_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(secret_question_field);
		secret_answer_field = new TextField<String>("secret_answer");
		secret_answer_field.setRequired(true).add(StringValidator.maximumLength(255));
		form.add(secret_answer_field);
		current_password_field = new PasswordTextField("current_password", new PropertyModel<String>(this, "current_password"));
		current_password_field.setRequired(false).add(StringValidator.minimumLength(8));
		form.add(current_password_field);
		new_password_field = new PasswordTextField("new_password", new PropertyModel<String>(this,"new_password"));
		new_password_field.setRequired(false).add(StringValidator.minimumLength(8));
		form.add(new_password_field);
		new_password_confirmation_field = new PasswordTextField("new_password_confirmation", new PropertyModel<String>(this,"new_password_confirmation"));
		new_password_confirmation_field.setRequired(false).add(StringValidator.minimumLength(8));
		form.add(new_password_confirmation_field);
		form.add(new Button("edit", new ResourceModel("edit")));
		form.add(new AbstractFormValidator() {
			
			@Override
			public void validate(Form<?> form) {
				User user = MercadoLibreSession.get().getUser();
				String curPass = current_password_field.getConvertedInput();
				String newPass = new_password_field.getConvertedInput();
				String newPassConf = new_password_confirmation_field.getConvertedInput();
				
				if(curPass != null  && user.getPassword().equals(current_password_field.getInput())){
					if(newPass == null || newPassConf == null){
						System.out.println("aca4");
						form.error(getString("completeNewPassword"));
						return;
					}
					if(newPass != null && newPassConf != null && newPass.equals(newPassConf)){
						return;
					}else{
						System.out.println("aca3");
						form.error(getString("matchPassword"));
						return;
					}
				}else if(curPass != null){
					System.out.println("aca2");
					form.error(getString("wrongPassword"));
					return;
				}else if(newPass != null || newPassConf != null){
					System.out.println("aca");
					form.error(getString("completeNewPassword"));
					return;
				}
			}
			
			@Override
			public FormComponent<?>[] getDependentFormComponents() {
				return new FormComponent[] {first_name_field,last_name_field,secret_question_field,secret_answer_field,current_password_field,new_password_field,new_password_confirmation_field};
			}
			
		});
		add(form);
	}
}
